package Reflection.Constructor;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * @author zxc
 * @date 2023/04/07 23:20
 **/
public class NewInstanceDemo {
    // T newInstance(Object... initargs)：根据指定的构造方法创建对象
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException,
            IllegalAccessException, InvocationTargetException, InstantiationException {
        System.out.println("获取公共,有参的类构造方法===========>");
        method1();
        System.out.println("获取公共,无参的类构造方法===========>");
        method2();
        System.out.println("获取公共,有参的类构造方法===========>");
        method3();
        System.out.println("获取私有类构造方法===========>");
        method4();
    }
    // 获取private类构造方法，调用Instance方法，来创建该类对应的实例对象;
    // 即，在调用private类构造方法，来创建对象实例前，需要使用setAccessible方法，取消该private类构造方法的访问检查;
    private static void method4() throws ClassNotFoundException, NoSuchMethodException,
            InstantiationException, IllegalAccessException, InvocationTargetException {
        //获取一个私有的构造方法并创建对象
        //1.获取class对象
        Class clazz = Class.forName("Reflection.Constructor.Student");

        //2.获取一个私有化的构造方法.
        Constructor constructor = clazz.getDeclaredConstructor(String.class);

        //被private修饰的成员,不能直接使用的
        //如果用反射强行获取并使用,需要临时取消访问检查
        constructor.setAccessible(true);

        //3.直接创建对象
        Student student = (Student) constructor.newInstance("zhangsan");

        System.out.println(student);
    }

    private static void method3() throws ClassNotFoundException,
            InstantiationException, IllegalAccessException {
        //简写格式
        //1.获取class对象
        Class clazz = Class.forName("Reflection.Constructor.Student");

        //2.在Class类中,有一个newInstance方法,可以利用空参直接创建一个对象
        Student student = (Student) clazz.newInstance();
        //这个方法现在已经过时了,了解一下

        System.out.println(student);
    }
    // 获取公共类构造方法，调用Instance方法，来创建该类对应的实例对象;
    private static void method2() throws ClassNotFoundException, NoSuchMethodException,
            InstantiationException, IllegalAccessException, InvocationTargetException {
        //1.获取class对象
        Class clazz = Class.forName("Reflection.Constructor.Student");

        //2.获取构造方法对象
        Constructor constructor = clazz.getConstructor();

        //3.利用空参来创建Student的对象
        Student student = (Student) constructor.newInstance();

        System.out.println(student);
    }
    // 获取公共类构造方法，调用Instance方法，来创建该类对应的实例对象;
    private static void method1() throws ClassNotFoundException, NoSuchMethodException,
            InstantiationException, IllegalAccessException, InvocationTargetException, InvocationTargetException {
        //1.获取class对象
        Class clazz = Class.forName("Reflection.Constructor.Student");

        //2.获取构造方法对象
        Constructor constructor = clazz.getConstructor(String.class, int.class);

        //3.利用newInstance创建Student的对象
        Student student = (Student) constructor.newInstance("杨天真", 22);

        System.out.println(student);
    }
}
